<template>
  <div class="root">
    <img src="@/assets/image/login/log.png" class="imgBoxA" alt="">
    <div></div>
    <div class="loginBox">
      <div class="content">
        <div class="titleBox">麒麟操作系统测试工具</div>
        <div class="formBox">
          <el-form
            autoComplete="on"
            :model="loginForm"
            :rules="loginRules"
            ref="loginForm"
            label-position="left"
          >
            <div style="text-align: center"></div>
            <el-form-item prop="username">
              <el-input
                name="phone"
                type="text"
                v-model="loginForm.username"
                placeholder="用户名"
              >
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                name="password"
                :type="pwdType"
                @keyup.enter.native="handleLogin"
                v-model="loginForm.password"
                placeholder="密码"
              >
              </el-input>
            </el-form-item>
            <el-form-item style="margin-bottom: 60px; text-align: center">
              <el-button
                style="width: 100%"
                type="primary"
                :loading="loading"
                @click.native.prevent="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    
  </div>
</template>

<script>
import md5 from 'js-md5';
import { isvalidUsername } from "@/utils/validate";
import { setSupport, getSupport, setCookie, getCookie } from "@/utils/support";
import { getToken, setToken, removeToken } from "@/utils/auth";
import request from "@/utils/request";
export default {
  name: "login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error("请输入正确的用户名"));
      } else {
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error("密码不能小于3位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "admin",
        password: "admin123",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePass },
        ],
      },
      loading: false,
      pwdType: "password",
    };
  },
  created() {
  
  },
  methods: {
   
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          let newForm = JSON.parse(JSON.stringify(this.loginForm));
          // newForm.password=md5( newForm.password)
          this.loading = true;
          this.$store
            .dispatch("Login", newForm)
            .then((res) => {
                 this.$router.push({ path: "/home" });
              console.log("success", getToken());
              this.loading = false;
              setCookie("user", this.loginForm.username, 15);
              setCookie("password",  newForm.password, 15);
           
              // if (res.code === 200) {
               
              // } else {
              //   this.$router.push({ path: "/" });
              // }
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.root {
  height: 100%;
  background-image: url("@/assets/image/login/loginBj.png");
  background-size: 100% 100%;
  display: flex;
  justify-content: space-between;
  position: relative;
  .imgBoxA{
    width: 84px;
    height: 117px;
    position: absolute;
    left: 40px;
    top: 40px;
  }
  .loginBox {
    width: 40%;
    background: #fff;
    display: flex;
    justify-content: center;
    .content {
      margin-top: 20%;
      width: 60%;
      .titleBox{
        text-align: center;
        font-size: 32px;
        font-weight: 700;
        margin-bottom: 60px;
      }
      .imgBox {
        width: 504px;
        height: 234px;
      }
      .formBox {
        margin-top: 20px;
      }
      /deep/ .el-form-item__content {
        width: 100%;
      }
      /deep/ .el-input__inner {
        height: 50px !important;
      }
      /deep/ .el-button {
        background: #00519d;
        border-color: #00519d;
      }
    }
  }
}
</style>
